import { useTokenStore } from "@/stores/modules/token";
const tokenKey = "Access-Token";
const refreshTokenKey = "Refresh-Token";

/**获取token */
export const getAccessToken = (): string | null => {
  return useTokenStore().access_token;
};
/**获取bearer token */
export function getBearerAccessToken() {
  var token = getAccessToken();
  return token ? "bearer " + token : null;
}
/**设置token */
export const setAccessToken = (token: string) => {
  useTokenStore().$patch({
    access_token: token
  });
};

/**获取刷新token */
export function getRefreshToken() {
  return useTokenStore().refresh_token;
}
/**设置刷新token */
export function setRefreshToken(refreshToken: string) {
  useTokenStore().$patch({
    refresh_token: refreshToken
  });
}

/**清除token */
export const clearToken = () => {
  useTokenStore().clearToken();
};

export const getStroage = (): Storage => {
  return localStorage;
};

/**
 * 将object对象转换为url参数
 * @param object 需要转换的对象
 * @param questionMark 是否需要?
 * @returns url
 */
export function objectToUrlParams(object: any, questionMark = false) {
  if (!object) return "";
  let url = "";
  Object.keys(object).forEach((key: string) => {
    if (object[key]) url += "&" + key + "=" + object[key];
  });
  if (!url) return "";
  return questionMark ? "?" + url.substring(1) : url;
}

/**
 * 生成UUid
 * @returns 返回32位uuid
 */
export function generateUUId() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    var r = (Math.random() * 16) | 0,
      v = c == "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}

/**
 * 数组元素交换位置
 * @param {array} arr 数组
 * @param {number} index1 添加项目的位置
 * @param {number} index2 删除项目的位置
 * index1和index2分别是两个数组的索引值，即是两个要交换元素位置的索引值，如1，5就是数组中下标为1和5的两个元素交换位置
 */
export function swapArray(arr: any[], index1: number, index2: number) {
  arr[index1] = arr.splice(index2, 1, arr[index1])[0];
  return arr;
}

/**
 * @description: 处理参数拼接的问题
 * @param {any} object 数据对象
 * @param {string} eliminate 剔除对象
 * @returns {any} 返回过滤对象
 * @author: ZhongShiGui
 */
export function pointUrlParam(object: any, eliminate: string): any {
  Reflect.deleteProperty(object, eliminate);
  return object;
}

/**
 * 获取url参数
 * @param variable 参数名
 * @returns
 */
export function getQueryVariable(variable: string) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return false;
}

/**
 * @description: 获取url参数
 * @param {string} key 参数key
 */
export function getUrlParams(key: string) {
  let reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
  let r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}

/**
 * @description 获取当前时间对应的提示语
 * @return string
 */
export function getTimeState() {
  // 获取当前时间
  let timeNow = new Date();
  // 获取当前小时
  let hours = timeNow.getHours();
  // 判断当前时间段
  if (hours >= 6 && hours <= 10) return `早上好 ⛅`;
  if (hours >= 10 && hours <= 14) return `中午好 🌞`;
  if (hours >= 14 && hours <= 18) return `下午好 🌞`;
  if (hours >= 18 && hours <= 24) return `晚上好 🌛`;
  if (hours >= 0 && hours <= 6) return `凌晨好 🌛`;
}
